package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.IFixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;
@ParentPackage("json-default")
@Namespace("/")
@Actions
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {
	
	@Autowired
	private IFixedAreaService fixedAreaService;
	
	@Action(value="fixedArea_save",results={@Result(type="redirect",location="./pages/base/fixed_area.html")})
	public String save(){
		fixedAreaService.save(model);	//BaseAction返回的模型对象是model,不要再写成fixedArea对象
		return SUCCESS;
	}
	
	//属性驱动
	private String[] customerIds;
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	
	//将选中的客户关联到指定的定区内
	@Action(value="fixedArea_associationCustomersToFixedArea",results={@Result(type="redirect",location="./pages/base/fixed_area.html")})
	public String associationCustomersToFixedArea(){
		String customerIdStr=StringUtils.join(customerIds, ",");
		WebClient.create("http://localhost:9002/crm_management/services/customerService"
				+ "/associationcustomerstofixedarea?customerIdStr="
				+customerIdStr+"&fixedAreaId="+model.getId())
				.type(MediaType.APPLICATION_JSON)
				.put(null);	//不太理解	???
		return SUCCESS;
	}
	
	//查询已关联定区客户
	@Action(value="fixedArea_findHasAssociationFixedAreaCustomers",results={@Result(type="json")})
	public String findHasAssociationFixedAreaCustomers(){
		//使用WebClient调用WebService接口
		Collection<? extends Customer> collection = WebClient
			.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"+model.getId())
			.accept(MediaType.APPLICATION_JSON)
			.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	//查询未关联定区客户
	@Action(value="fixedArea_findNoAssociationCustomers",results={@Result(type="json")})
	public String findNoAssociationCustomers(){
		//使用WebClient调用WebService接口
		Collection<? extends Customer> collection = WebClient
			.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
			.accept(MediaType.APPLICATION_JSON)
			.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	@Action(value="fixedArea_pageQuery",results={@Result(type="json")})
	public String pageQuery(){
		//创建条件查询对象
		Specification<FixedArea> specification=new Specification<FixedArea>(){
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				List<Predicate> list=new ArrayList<Predicate>();
				if(StringUtils.isNotBlank(model.getId())){
					Predicate p1=cb.equal(root.get("id").as(String.class),model.getId());
					list.add(p1);
//					System.out.println("p1================================="+p1);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					Predicate p2=cb.like(root.get("company").as(String.class),"%"+model.getCompany()+"%");
					list.add(p2);
//					System.out.println("p2===================================="+p2);
				}
//				System.out.println("list==================================="+list);
				return cb.and(list.toArray(new Predicate[0]));
			}};
		
		Pageable pageable=new PageRequest(page-1, rows);
//		System.out.println("pageable============================="+pageable);
		Page<FixedArea> pageData=fixedAreaService.findPageData(specification,pageable);
//		System.out.println("pageData============================="+pageData);
		//使用pushPageDataToValueStack()方法将pageData压入栈
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	
	//获取删除时传过来的id字符串
	private String ids;
	public void setIds(String ids) {
		this.ids = ids;
	}

	//删除定区功能
	@Action(value="fixedArea_delete",results={@Result(type="redirect",location="./pages/base/fixed_area.html")})
	public String delete(){
		String[] idArray=ids.split(",");
		for (String idStr : idArray) {
			fixedAreaService.delete(idStr);
		}
		return SUCCESS;
	}
	
	//属性驱动
	private Integer courierId; 
	private Integer takeTimeId;
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(type="redirect",location="./pages/base/fixed_area.html")})
	public String associationCourierToFixedArea(){
		fixedAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}
		
}
